home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 4425 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.8 KB

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Allocation of memory
  5. Date: 4 Feb 1996 07:38:45 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4f2ju5INNlho@keats.ugrad.cs.ubc.ca>
  8. References: <4evre9$8r4@news.rz.uni-passau.de>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10. Keywords: realloc
  11.  
  12. In article <4evre9$8r4@news.rz.uni-passau.de>,
  13. Klaus Berndl <berndl@piligrim.uni-passau.de> wrote:
  14. >today i have a not-RPC-problem, but perhaps you can solve it:
  15. >
  16. >Please look at the foolwing short piece of code:
  17.  
  18. Let's not and say we did!
  19.  
  20. >Now my questions:
  21. >
  22. >1) How is it possible, that my machine (Sparc classic) allocates 100MB
  23. >   Storage? I`m sure i have not so much main memory installed. Does it
  24. >   use swap-file??
  25.  
  26. It's called "virtual memory". Here is one hypothesis on how it is done: Your
  27. malloc() calls the brk() system call, which tells the operating system that you
  28. have 100MB more worth of pages added to your data segment. These are marked
  29. "not-present". It is when you access these these pages that the OS will
  30. actually try to assign to them a piece of virtual memory. It's sort of an
  31. "allocate on write".
  32.  
  33. So you won't see any thrashing until you start using the memory.
  34.  
  35. >2) With ALLOCSIZE set to 100000000 the program works correct. But if i set
  36. >   ALLOCSIZE to 300000000, i reach the following output:
  37. >
  38. >     Klaus
  39. >
  40. >      t-realloc failed
  41. >
  42. >     test1
  43. >     Bus Error (core dumped)
  44.  
  45. It could be that the OS will refuse to honor a brk() call that would result in
  46. more pages being allocated than the free pages in the virtual memory pool.
  47.  
  48. >   What is 'realloc' doing exactly? Normally if realloc can`t allocate the
  49. >   requested memory it returns NULL and doesn`t allocate memory. Why the 
  50. >   second realloc-call (x = ...) fails? There must be 1000000 Byte memory
  51. >   because of the first try with ALLOCSIZE 100000000 works!
  52.  
  53. Realloc uses the same routines as malloc() to allocate heap storage. When it
  54. can't allocate, it will use the brk() system call to move the "break" address,
  55. which marks the end of the addresses that exist in the process.
  56.  
  57. Check how much virtual space your system has. If it's not enough for the
  58. kind of allocation you are doing in your project, you need to add more swap
  59. space and/or buy more RAM.
  60.  
  61.  
  62. >3) We are comming to the point: I must succesive realloc storage for a 
  63. >   certain variable v. And if realloc fails, i want have the recent correct
  64. >   memory allocation for v containing the right value (look at 'temp' in the
  65. >   code). I need a save solution! 
  66.  
  67. Then write your own realloc() routine or macro which allocates new storage, and
  68. if successful, copies the data into it and gives you the new pointer, and then
  69. frees the original copy. Give it different semantics, something like:
  70.  
  71.     int my_realloc(void **old2new, size_t size);
  72.  
  73. If my_realloc() fails, it can be set up so that it will _not_ change the value
  74. of the void *old2new and returns some error code, otherwise it will update the
  75. value of the pointer.
  76.  
  77.  
  78. I wish I could tell you that unsuccessful realloc() does no harm to the
  79. original allocated block, but I can't say that this is guaranteed on every
  80. system.
  81.  
  82. The manual page on the HP-UX system does make such a guarantee:
  83.  
  84.  RETURN VALUE
  85.       Upon successful completion, malloc(), realloc(), and calloc() return a
  86.       pointer to space suitably aligned (after possible pointer coercion)
  87.       for storage of any type of object.  Otherwise, they return a NULL
  88.       pointer.  If realloc() returns a NULL pointer, the memory pointed to
  89.       by the original pointer is left intact.
  90.  
  91. It probably works the same way on other UNIX systems. So as long as you
  92. remember what the old pointer was, you should be able to keep using it if
  93. realloc() failed.  
  94. -- 
  95.  
  96.